import SyRequest from './request'
import { BASE_URL, TIME_OUT } from './config'
import localStorage from '@/utils/local-storage'
import useMainStore from '@/stores/main/main'
import type { RawAxiosRequestHeaders } from 'axios'
import notifyFn from '@/utils/res-notify'
import router from '@/router'
export default new SyRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestSuccessFn: (config: any) => {
      const token = localStorage.get('token')
      if (token) {
        ;(
          config.headers as RawAxiosRequestHeaders
        ).Authorization = `Bearer ${token}`
        // ;(config.headers as AxiosHeaders).set(
        //   'Authorization',
        //   `Bearer ${token}`
        // )
      }
      useMainStore().showLoading = true
      return config
    },
    responseSuccessFn: (res: any) => {
      useMainStore().showLoading = false
      if (res.msg === 'token已过期请重新登录') {
        notifyFn({
          message: res.msg,
          type: res.code
        })
        router.push('/login')
      }
      return res
    }
  }
})
